//
//  NewCityGoodsClassify.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/13.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class NewCityGoodsClassify: BaseViewController {
    
    /*
     
     http://image.9bianli.com
     
     http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05531746494804292.jpg
     
     http://image.9bianli.com:33/Files/A01/Image/OtherResource/913A3791-8E22-4ABF-A75C-4218541E4808.jpg
     
     http://image.9bianli.com:33/Files/A01/Image/OtherResource/5F727CE1-40EF-49AC-ABEA-F873D67580F8.jpg
     
     http://image.9bianli.com:33/Files/A01/Image/OtherResource/A03449C2-0A51-4AA7-B851-218DD91B9A05.jpg
     
     http://image.9bianli.com:33/Files/A01/Image/OtherResource/2D4749CD-8135-4EA1-BE85-F83B9CDE6E93.jpg
     
     http://image.9bianli.com:33/Files/A01/Image/OtherResource/1BE03444-25D9-4FD3-809E-DFE0F545BB15.jpg
     
     http://image.9bianli.com:33/Files/A01/Image/OtherResource/78DDE707-F2B2-474E-AFF3-E5187107F87C.jpg
     
     
     
     */
    
    var dataModels = [ClassifyModel]()
    
    var models = [OneItemModel]()
    
    var images = ["http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05531746494804292.jpg",
                  "http://image.9bianli.com:33/Files/A01/Image/OtherResource/913A3791-8E22-4ABF-A75C-4218541E4808.jpg",
                  "http://image.9bianli.com:33/Files/A01/Image/OtherResource/5F727CE1-40EF-49AC-ABEA-F873D67580F8.jpg",
                  "http://image.9bianli.com:33/Files/A01/Image/OtherResource/A03449C2-0A51-4AA7-B851-218DD91B9A05.jpg",
                  "http://image.9bianli.com:33/Files/A01/Image/OtherResource/2D4749CD-8135-4EA1-BE85-F83B9CDE6E93.jpg",
                  "http://image.9bianli.com:33/Files/A01/Image/OtherResource/1BE03444-25D9-4FD3-809E-DFE0F545BB15.jpg",
                  "http://image.9bianli.com:33/Files/A01/Image/OtherResource/78DDE707-F2B2-474E-AFF3-E5187107F87C.jpg"
    ]

    
    var names = ["水",
                 "白酒",
                 "啤酒",
                 "葡萄酒",
                 "洋酒",
                 "黄酒",
                 "饮料"
    ]
    
    
    var des = [  "畅饮钓鱼岛 坚定爱国心",
                 "香醇淡雅 与君子同饮",
                 "纯正进口啤酒 给你更好的味道",
                 "果香浓郁 来自名庄的精挑细选",
                 "不一样的格调 别样生活",
                 "传承千年 好酒养身",
                 "更好的水 给更好的自己"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "酒便利"
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: NewCityWinHomeCell.self), bundle: nil), forCellReuseIdentifier: NewCityWinHomeCell.identifer())
        
        plainTableView.separatorStyle = .none
        
        
        weak var weakSelf = self
        plainTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadData()
            
        }
        
        plainTableView.mj_header.beginRefreshing()

        

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

 
}



extension NewCityGoodsClassify{
    
    func loadData() {
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=goods_class&a=index", parameters: nil) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                
                for dic in responseObject!["datas"]!["class_list"] as! [[String : AnyObject]]{
                    
                    let temp = ClassifyModel(dic: dic)
                    
                    if temp.child.count > 0{
                        
                        self!.dataModels.append(temp)
                        
                        
                    }
                    
                    
                }
                
                if self!.dataModels.count > 0{
                    
                    for item in self!.dataModels[0].child[1].grandson{
                    
                        self!.models.append(item)

                    }
                    
                    
                    self!.plainTableView.reloadData()
                    
                }
                
                self?.plainTableView.mj_header.endRefreshing()
                
                
                
            }else{
                
                self?.plainTableView.mj_header.endRefreshing()

                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
            
            
        }
        
        
    }
    
    
    
    
}







extension NewCityGoodsClassify{
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
    
        
        return 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if models.count > 0 {
//           return models.count
            return 7
        } else {
            return 0
        }

        
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let tableCell : NewCityWinHomeCell =  tableView.dequeueReusableCell(withIdentifier: NewCityWinHomeCell.identifer()) as! NewCityWinHomeCell
        
        let temp = models[indexPath.row]
        
        temp.gc_image = images[indexPath.row]
        
        temp.gc_name = names[indexPath.row]
        
        temp.gc_description = des[indexPath.row]
        
        tableCell.model = temp
        
        
        return tableCell
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        
        if indexPath.row == 0 {
            
            return KScreenW / 128.0 * 48.0
            
        } else {
            
            return KScreenW / 75.0 * 45.0

            
        }
        
    }
    
       
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        let temp = NewCityGoodsListVC()
        
        temp.seletIndex = indexPath.row
        
//        temp.goodId = models[indexPath.row].gc_id
        
        navigationController?.pushViewController(temp, animated: true)
        
        
    }
    
    
    
    
    
    
}

